/**
 * 
 */
package org.vsg.stock.core.domain;

import java.text.DecimalFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.vsg.stock.core.KLineStock.Attribute;
import org.vsg.stock.core.kline.DailyKLineStock;

/**
 * @author vison
 *
 */
@Entity
@Table(name = "KLINE_DAILY")
public class StockDailyKLine implements DailyKLineStock {
	
	static public final short OPEN = 1;
	
	static public final short CLOSE = 2;
	
	static public final short HIGH = 3;
	
	static public final short LOW = 4;
	
	static public final short VOL = 5;
	
	static public final short TRANSACTION_VALUE = 6;
	
	static public final short MIDDLE = 7;
	

	static private DecimalFormat nf = new DecimalFormat("##0.000");	
	
	private StockDailyKLinePK id;

	@Column(name="OPEN")
	private double open;

	@Column(name="HIGH")
	private double high;

	@Column(name="LOW")
	private double low;
	
	@Column(name="CLOSE")
	private double close;

	@Column(name="YIELD_RATE")
	private double yieldRate;
	
	@Column(name="VOL")	
	private long vol;

	@Column(name="TRANSACTION_VALUE")
	private double transactionValue;

	@Column(name="EX_RIGHTS")
	private short exRights;

	

	@Override
	public Attribute[] sortAttrs() {
		// TODO Auto-generated method stub
		
		double bottom = this.open > this.close ? this.close : this.open;
		
		Attribute[] attrs = new Attribute[4];
		attrs[0] = Attribute.LOW;
		
		if ( bottom == this.open ) {
			attrs[1] = Attribute.OPEN;
			attrs[2] = Attribute.CLOSE;
		} else {
			attrs[1] = Attribute.CLOSE;
			attrs[2] = Attribute.OPEN;
		}
		attrs[3] = Attribute.HIGH;
		
		return attrs;
	}
	

	@Override
	public Date getDate() {
		// TODO Auto-generated method stub
		return id.getDate();
	}

	public StockDailyKLinePK getId() {
		return id;
	}

	public void setId(StockDailyKLinePK id) {
		this.id = id;
	}

	@Override
	public double getOpen() {
		return open;
	}

	public void setOpen(double open) {
		this.open = open;
	}

	@Override
	public double getHigh() {
		return high;
	}

	public void setHigh(double high) {
		this.high = high;
	}

	@Override
	public double getLow() {
		return low;
	}

	public void setLow(double low) {
		this.low = low;
	}

	public double getClose() {
		return close;
	}

	public void setClose(double close) {
		this.close = close;
	}
	
	@Override
	public double getBottom() {
		// TODO Auto-generated method stub
		return this.open > this.close ? this.close : this.open;
	}


	@Override
	public double getTop() {
		// TODO Auto-generated method stub
		return this.open > this.close ? this.open : this.close;
	}


	public double getYieldRate() {
		return yieldRate;
	}

	public void setYieldRate(double yieldRate) {
		this.yieldRate = yieldRate;
	}

	@Override
	public long getVol() {
		return vol;
	}

	public void setVol(long vol) {
		this.vol = vol;
	}

	@Override
	public double getTransactionValue() {
		return transactionValue;
	}

	public void setTransactionValue(double transactionValue) {
		this.transactionValue = transactionValue;
	}

	public short getExRights() {
		return exRights;
	}

	public void setExRights(short exRights) {
		this.exRights = exRights;
	}
	
	public double getMiddle() {
		double value = ( this.open + this.close) / 2;
		String valueStr = nf.format( value );
		
		return Double.parseDouble( valueStr );
	}

	@Override
	public double getAverage() {
		double value = transactionValue / vol;
		
		String valueStr = nf.format( value );

		return Double.parseDouble( valueStr );
	}

	@Override
	public String toString() {
		return "StockDailyKLine [close=" + close + ", exRights=" + exRights
				+ ", high=" + high + ", id=" + id + ", low=" + low + ", open="
				+ open + ", transactionValue=" + transactionValue + ", vol="
				+ vol + ", yieldRate=" + yieldRate + "]";
	}
	

	
}
